﻿using Microsoft.Extensions.Logging;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WMS.Model;
using WMS.Repository.IRepository;

namespace WMS.Repository
{
    public class RoleMenuRepository : BaseRepository<RoleMenuModel>, IRoleMenuRepository
    {
        public RoleMenuRepository(ISqlSugarClient db, ILogger<RoleMenuRepository> logger) : base(db, logger)
        {
        }

        /// <summary>
        /// 获取角色的菜单列表
        /// </summary>
        public async Task<List<RoleMenuModel>> GetRoleMenuById(long roleId)
        {
            try
            {
                return await _db.Queryable<RoleMenuModel>().Where(m => m.RoleId == roleId).ToListAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"获取角色菜单列表失败 - 角色ID: {roleId}");
                throw;
            }
        }

        /// <summary>
        /// 删除角色菜单
        /// </summary>
        public async Task<int> DeleteRoleMenus(long roleId, List<long> menuIds)
        {
            try
            {
                return await _db.Deleteable<RoleMenuModel>()
                               .Where(rm => rm.RoleId == roleId && menuIds.Contains(rm.MenuId))
                               .ExecuteCommandAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"删除角色菜单失败 - 角色ID: {roleId}, 菜单数量: {menuIds?.Count ?? 0}");
                throw;
            }
        }

        /// <summary>
        /// 批量添加角色菜单
        /// </summary>
        public async Task<int> AddRoleMenus(List<RoleMenuModel> roleMenus)
        {
            try
            {
                return await _db.Insertable(roleMenus).ExecuteCommandAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"批量添加角色菜单失败 - 记录数量: {roleMenus?.Count ?? 0}");
                throw;
            }
        }

        /// <summary>
        /// 添加单个角色菜单
        /// </summary>
        public async Task<int> AddRoleMenu(RoleMenuModel roleMenuModel)
        {
            try
            {
                return await _db.Insertable(roleMenuModel).ExecuteCommandAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"添加单个角色菜单失败 - 角色ID: {roleMenuModel?.RoleId}, 菜单ID: {roleMenuModel?.MenuId}");
                throw;
            }
        }

        /// <summary>
        /// 获取角色已拥有的菜单ID列表
        /// </summary>
        public async Task<List<long>> GetRoleMenuIds(long roleId)
        {
            try
            {
                return await _db.Queryable<RoleMenuModel>().Where(m => m.RoleId == roleId).Select(m => m.MenuId).ToListAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"获取角色菜单ID列表失败 - 角色ID: {roleId}");
                throw;
            }
        }

        /// <summary>
        /// 根据角色ids获取角色菜单列表
        /// </summary>
        public async Task<List<RoleMenuModel>> GetListByRoleIdsAsync(List<long> roleIds)
        {
            try
            {
                return await _db.Queryable<RoleMenuModel>()
                    .Where(m => roleIds.Contains(m.RoleId))
                    .ToListAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"根据角色ID列表获取菜单失败 - 角色数量: {roleIds?.Count ?? 0}");
                throw;
            }
        }
    }
}